import requests
from pyquery import PyQuery as pq
import re
import json


def doulist_crawler(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/58.0.3029.110 Safari/537.3',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    }
    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        print(f"请求失败，状态码：{response.status_code}")
        return []

    doc = pq(response.text)
    doulist = []
    for item in doc(".doulist-item").items():
        detail_url = item(".title a").attr("href")
        title = item(".title a").text()
        rating_nums = item(".rating_nums").text()
        rating_count = int(re.search(r'\d+', item('.rating span:contains("人评价")').text()).group(0))

        abstract = item('div.abstract').text()
        info_mapping = {
            '导演': 'director',
            '主演': 'starring',
            '类型': 'genre',
            '制片国家/地区': 'region',
            '年份': 'year'
        }
        item_dict = {
            'detail_url': detail_url,
            'title': title,
            'rating_nums': rating_nums,
            'rating_count': rating_count
        }
        for keyword, key in info_mapping.items():
            match = re.search(f'{keyword}:([^\\n]+)', abstract)
            item_dict[key] = match.group(1).strip() if match else None

        doulist.append(item_dict)

    return doulist


if __name__ == "__main__":
    movie_list = doulist_crawler('https://www.douban.com/doulist/240962/')
    try:
        with open('movies.json', 'w', encoding='utf-8') as f:
            json.dump(movie_list, f, ensure_ascii=False, indent=4)
        print("电影信息已成功存储为 movies.json 文件。")
    except Exception as e:
        print(f"存储文件时出现错误：{e}")
